/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Screens;

import Controllers.HdSetoresJpaController;
import Controllers.HdTipoUserJpaController;
import Controllers.HdUsersJpaController;
import Entities.HdTipoUser;
import Entities.HdUsers;
import Entities.HdSetores;
import Util.Constantes;
import Util.EMFConection;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Douglas
 */
public class CadastroUsers extends javax.swing.JFrame {

    /**
     * Creates new form CadastroUsers
     */
    public CadastroUsers() {
        initComponents();
        
        HdTipoUserJpaController tipoUserJpaController = new HdTipoUserJpaController(EMFConection.getInstance());
        List<HdTipoUser> listTipousers = tipoUserJpaController.findHdTipoUserEntities();
        
        for (HdTipoUser hdTipoUser : listTipousers) {
            cbCodTipoUsers.addItem(hdTipoUser.getDeTipoUser());
        }
        
        HdSetoresJpaController hdSetoresJpaController = new HdSetoresJpaController(EMFConection.getInstance());
        List<HdSetores> listSetores = hdSetoresJpaController.findHdSetoresEntities();
        
        for (HdSetores hdSetor : listSetores) {
            cbCodSetor.addItem(hdSetor.getDeSetor());
        }
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbNomeUsers = new javax.swing.JLabel();
        tfNomeUsers = new javax.swing.JTextField();
        lbSenha = new javax.swing.JLabel();
        lbEmail = new javax.swing.JLabel();
        tfEmail = new javax.swing.JTextField();
        lbCodUsers = new javax.swing.JLabel();
        cbCodTipoUsers = new javax.swing.JComboBox();
        lbCodSetor = new javax.swing.JLabel();
        cbCodSetor = new javax.swing.JComboBox();
        pfSenha = new javax.swing.JPasswordField();
        btCadastrar = new javax.swing.JButton();
        btCancelar = new javax.swing.JButton();
        pfSenha1 = new javax.swing.JPasswordField();
        lbSenha1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Help Desk");
        setResizable(false);

        lbNomeUsers.setText("Nome do Usuário:");

        lbSenha.setText("Senha:");

        lbEmail.setText("Email:");

        lbCodUsers.setText("Tipo de Usuario:");

        lbCodSetor.setText("Setor:");

        btCadastrar.setText("Cadastrar");
        btCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCadastrarActionPerformed(evt);
            }
        });

        btCancelar.setText("Cancelar");
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        lbSenha1.setText("Confirme a senha:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btCadastrar)
                        .addGap(18, 18, 18)
                        .addComponent(btCancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(tfNomeUsers, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lbNomeUsers)
                                    .addComponent(lbEmail))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(pfSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lbSenha)
                                    .addComponent(lbCodUsers)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(tfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(cbCodTipoUsers, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(lbCodSetor)
                            .addComponent(pfSenha1, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                            .addComponent(lbSenha1)
                            .addComponent(cbCodSetor, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(43, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lbNomeUsers)
                            .addComponent(lbSenha))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfNomeUsers, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(pfSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lbSenha1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pfSenha1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbEmail)
                    .addComponent(lbCodUsers)
                    .addComponent(lbCodSetor))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbCodTipoUsers, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbCodSetor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btCadastrar)
                    .addComponent(btCancelar))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-545)/2, (screenSize.height-217)/2, 545, 217);
    }// </editor-fold>//GEN-END:initComponents

    private void btCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCadastrarActionPerformed
        HdUsersJpaController userJpaController = new HdUsersJpaController(EMFConection.getInstance());
        HdSetoresJpaController setoresJpaController = new HdSetoresJpaController(EMFConection.getInstance());
        HdTipoUserJpaController tipoUserJpaController = new HdTipoUserJpaController(EMFConection.getInstance());
        
        HdUsers user = new HdUsers();
        
        if(tfNomeUsers.getText().isEmpty()){
            JOptionPane.showMessageDialog(null , Constantes.NOME_EM_BRANCO);
        }else if (pfSenha.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, Constantes.SENHA_EM_BRANCO);
        }else if(!pfSenha.getText().equals(pfSenha1.getText())) {
            JOptionPane.showMessageDialog(null, Constantes.SENHA_NAO_CONINCIDEM, Constantes.SUCESSO, JOptionPane.INFORMATION_MESSAGE);
        }else if(!(userJpaController.findHdUsersByName(tfNomeUsers.getText()) == null)){
            JOptionPane.showMessageDialog(null, Constantes.USER_EXISTENTE, Constantes.SUCESSO, JOptionPane.INFORMATION_MESSAGE);
        }else {
            user.setCoTipoUser(tipoUserJpaController.findHdTipoUser(cbCodTipoUsers.getSelectedIndex()+1));
            user.setCoSetorUser(setoresJpaController.findHdSetores(cbCodSetor.getSelectedIndex()+1));
            user.setDeEmailUser(tfEmail.getText());
            user.setDeSenhaUser(pfSenha.getText());
            user.setNmUser(tfNomeUsers.getText());
            try{
                userJpaController.create(user);
                JOptionPane.showMessageDialog(null, Constantes.DADOS_INSERIDOS, Constantes.SUCESSO, JOptionPane.INFORMATION_MESSAGE);
            }catch(Exception e){
                JOptionPane.showMessageDialog(null, Constantes.ERRO_BANCO, Constantes.ERRO, JOptionPane.ERROR_MESSAGE);
            }
            this.dispose();
        }
    }//GEN-LAST:event_btCadastrarActionPerformed

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btCancelarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCadastrar;
    private javax.swing.JButton btCancelar;
    private javax.swing.JComboBox cbCodSetor;
    private javax.swing.JComboBox cbCodTipoUsers;
    private javax.swing.JLabel lbCodSetor;
    private javax.swing.JLabel lbCodUsers;
    private javax.swing.JLabel lbEmail;
    private javax.swing.JLabel lbNomeUsers;
    private javax.swing.JLabel lbSenha;
    private javax.swing.JLabel lbSenha1;
    private javax.swing.JPasswordField pfSenha;
    private javax.swing.JPasswordField pfSenha1;
    private javax.swing.JTextField tfEmail;
    private javax.swing.JTextField tfNomeUsers;
    // End of variables declaration//GEN-END:variables
}
